package com.tz.fruit.controller.backstage;

import com.tz.fruit.entity.Intro;
import com.tz.fruit.entity.Product;
import com.tz.fruit.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller(value="backIntroController")
@RequestMapping("/backstage/intro")
public class IntroController {

    @Autowired
    IProductService productService;

    /**
     * 根据商品Id查询所有简介
     */
    @RequestMapping("/list")
    public String list(Model model, Long pid){
        Product product = productService.findById(pid);
        List<Intro> intros = productService.findIntroByPid(pid);
        model.addAttribute("product",product);
        model.addAttribute("intros",intros);
        return "backstage/intro/list";
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/addPre")
    public String addPre(Model model,Long pid){
        //把对象放入request作用域
        model.addAttribute("pid",pid);
        return "/backstage/intro/add";
    }

    /**
     *  添加商品简介
     */
    @RequestMapping("/add")
//    public String add(Model model, @Validated/*(value = {Group1.class})*/@ModelAttribute Product product, BindingResult bindingResult){
    public String add(Model model, Intro intro){
//        if(bindingResult.hasErrors()){
//            List<ObjectError> errors = bindingResult.getAllErrors();
//            //把错误信息放入request作用域
//            model.addAttribute("errors",errors);
//            //查询所有推荐类型
//            RecState[] recStates = RecState.values();
//            //查询所有类别
//            List<Category> categories = productService.findAllCategory();
//            //把对象放入request作用域
//            model.addAttribute("recStates",recStates);
//            model.addAttribute("categories",categories);
//            return "backstage/product/add";
//        }
        productService.save(intro);
        return "redirect:list?pid="+intro.getProduct().getPid();
    }

    /**
     * 删除商品简介
     */
    @RequestMapping("/delete")
    public String delete(Long[] introId,Long pid){
        for(int i=0;i<introId.length;i++){
            productService.delByIntroId(introId[i]);
        }
        return "redirect:list?pid="+pid;
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping("/editPre")
    public String editPre(Model model,Long pid,Long introId){
        //根据商品id查询所有简介
        Intro intro = productService.findByIntroId(introId);
        //把对象放入request作用域
        model.addAttribute("pid",pid);
        model.addAttribute("intro",intro);
        return "/backstage/intro/edit";
    }

    /**
     * 更新商品简介
     */
    @RequestMapping("/edit")
    public String edit(Intro intro){
        productService.update(intro);
        return "redirect:list?pid="+intro.getProduct().getPid();
    }

    /**
     * 检查简介名称是否已经存在
     * @param exclude 原来的value，排除
     */
    @RequestMapping("/checkKey")
    public String checkKey(String key,String exclude,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if(key!=null && key.trim().length()>0){
            Intro intro = productService.findByKey(key);
            if(intro==null){
                out.print("true");
            }else if(null != exclude && exclude.trim().length()>0){
                if(exclude.equals(intro.getKey())){
                    out.print("true");
                }else{
                    out.print("false");
                }
            }else{
                out.print("false");
            }
        }
        return null;
    }
}
